
/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.postorder.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxk.oms.postorder.domain.OmsPostOrder;
import com.lxk.oms.postorder.domain.bo.OmsPostOrderBo;
import com.lxk.oms.postorder.domain.enums.PostOrderEnums;
import com.lxk.oms.postorder.domain.vo.OmsPostOrderVo;
import com.lxk.oms.postorder.mapper.OmsPostOrderMapper;
import com.lxk.oms.postorder.service.IOmsPostOrderService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单售后Service业务层处理
 *
 * @author LXK-RY5
 * @date 2024-09-28
 */
@RequiredArgsConstructor
@Service
public class OmsPostOrderServiceImpl implements IOmsPostOrderService {

    private final OmsPostOrderMapper baseMapper;


    @Override
    public Boolean existByOrderNo(String orderNo, PostOrderEnums.PostOrderStatus... status) {
        Assert.notBlank(orderNo);
        return baseMapper.exists(Wrappers.<OmsPostOrder>lambdaQuery()
            .eq(OmsPostOrder::getOrderSn, orderNo)
            .in(OmsPostOrder::getStatus, StreamUtils.toList(CollUtil.newArrayList(status), PostOrderEnums.PostOrderStatus::getPostOrderStatus)));
    }

    /**
     * 查询IdList订单售后Map
     */
    @Override
    public Map<Long, OmsPostOrderVo> queryMapByIdList(List<Long> idList) {
        return this.queryByIdList(idList).stream().collect(Collectors.toMap(OmsPostOrderVo::getId, v -> v));
    }

    /**
     * 查询订单售后Map
     */
    @Override
    public Map<Long, OmsPostOrderVo> queryMap(OmsPostOrderBo bo) {
        return this.queryList(bo).stream().collect(Collectors.toMap(OmsPostOrderVo::getId, v -> v));
    }

    /**
     * 查询订单售后IdList
     */
    @Override
    public List<OmsPostOrderVo> queryByIdList(List<Long> idList) {
        if (CollUtil.isEmpty(idList)) {
            return CollUtil.newArrayList();
        }
        LambdaQueryWrapper<OmsPostOrder> lqw = Wrappers.lambdaQuery();
        lqw.in(OmsPostOrder::getId, idList);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询订单售后
     *
     * @param id 主键
     * @return 订单售后
     */
    @Override
    public OmsPostOrderVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }


    /**
     * 更新订单售后
     *
     * @return 订单售后
     */
    @Override
    public Boolean updateStatus(Long id, PostOrderEnums.PostOrderStatus src, PostOrderEnums.PostOrderStatus dst) {
        OmsPostOrder update = new OmsPostOrder();
        update.setStatus(dst.getPostOrderStatus());
        int i = baseMapper.update(update, Wrappers.<OmsPostOrder>lambdaQuery().eq(OmsPostOrder::getStatus, src.getPostOrderStatus()).eq(OmsPostOrder::getId, id));
        return i > 0;
    }

    /**
     * 查询订单售后
     *
     * @param postOrderNo     售后订单编号
     * @param postOrderStatus
     * @return 订单售后
     */
    @Override
    public OmsPostOrderVo queryByNo(String postOrderNo, Long userId, Integer postOrderStatus) {
        return baseMapper.selectVoOne(Wrappers.<OmsPostOrder>lambdaQuery()
            .eq(OmsPostOrder::getStatus, postOrderStatus).eq(OmsPostOrder::getPostOrderNo, postOrderNo).eq(OmsPostOrder::getUserId, userId));
    }

    /**
     * 查询订单售后
     *
     * @param postOrderNo
     * @return
     */
    @Override
    public OmsPostOrderVo queryByNo(String postOrderNo, Integer postType) {
        return baseMapper.selectVoOne(Wrappers.<OmsPostOrder>lambdaQuery()
            .eq(OmsPostOrder::getPostType, postType)
            .eq(OmsPostOrder::getPostOrderNo, postOrderNo));
    }

    /**
     * 分页查询订单售后列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 订单售后分页列表
     */
    @Override
    public TableDataInfo<OmsPostOrderVo> queryPageList(OmsPostOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OmsPostOrder> lqw = buildQueryWrapper(bo);
        Page<OmsPostOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的订单售后列表
     *
     * @param bo 查询条件
     * @return 订单售后列表
     */
    @Override
    public List<OmsPostOrderVo> queryList(OmsPostOrderBo bo) {
        LambdaQueryWrapper<OmsPostOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OmsPostOrder> buildQueryWrapper(OmsPostOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OmsPostOrder> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(OmsPostOrder::getId);
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            OmsPostOrder::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            OmsPostOrder::getUpdateTime, params.get("beginUpdateTime"), params.get("endUpdateTime"));
        lqw.eq(bo.getMerId() != null, OmsPostOrder::getMerId, bo.getMerId());
        lqw.eq(StringUtils.isNotBlank(bo.getPostOrderNo()), OmsPostOrder::getPostOrderNo, bo.getPostOrderNo());
        lqw.eq(bo.getPostType() != null, OmsPostOrder::getPostType, bo.getPostType());
        lqw.eq(bo.getOrderId() != null, OmsPostOrder::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderSn()), OmsPostOrder::getOrderSn, bo.getOrderSn());
        lqw.eq(bo.getOrderType() != null, OmsPostOrder::getOrderType, bo.getOrderType());
        lqw.eq(bo.getUserId() != null, OmsPostOrder::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getUserIp()), OmsPostOrder::getUserIp, bo.getUserIp());
        lqw.eq(StringUtils.isNotBlank(bo.getReasons()), OmsPostOrder::getReasons, bo.getReasons());
        lqw.eq(StringUtils.isNotBlank(bo.getExplains()), OmsPostOrder::getExplains, bo.getExplains());
        lqw.eq(StringUtils.isNotBlank(bo.getExplainImg()), OmsPostOrder::getExplainImg, bo.getExplainImg());
        lqw.eq(bo.getStatus() != null, OmsPostOrder::getStatus, bo.getStatus());
        lqw.eq(bo.getRefundAmount() != null, OmsPostOrder::getRefundAmount, bo.getRefundAmount());
        lqw.eq(bo.getRefundCount() != null, OmsPostOrder::getRefundCount, bo.getRefundCount());
        return lqw;
    }

    /**
     * 新增订单售后
     *
     * @param bo 订单售后
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(OmsPostOrderBo bo) {
        OmsPostOrder add = MapstructUtils.convert(bo, OmsPostOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单售后
     *
     * @param bo 订单售后
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(OmsPostOrderBo bo) {
        OmsPostOrder update = MapstructUtils.convert(bo, OmsPostOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OmsPostOrder entity) {
    }

    /**
     * 校验并批量删除订单售后信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
